package cn.itcast.order.web;

import cn.itcast.feign.api.UserClient;
import cn.itcast.feign.pojo.User;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@RequestMapping("order")
public class OrderController {

   @Autowired
   private OrderService orderService;
   @Resource
   private RestTemplate restTemplate;

   @Resource
   private UserClient userClient;

    @GetMapping("{orderId}")
    @ResponseBody
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) {
        // 根据id查询订单并返回
        Order order = orderService.queryOrderById(orderId);
        //远程调用
        Long userId = order.getUserId();
        //String url = "http://userservice/user/"+userId;
       // User user = restTemplate.getForObject(url, User.class);

        //利用fegin调用
        User user = userClient.findById(userId);
        order.setUser(user);
        return order;
    }
}
